package cn.jekun.shop.web.action;

import java.net.URLDecoder;

import javax.annotation.Resource;







import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.bean.MemberSession;
import cn.jekun.shop.web.service.ShopService;
/**
 * 商家
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class ShopAction1 extends ShopBaseAction{
	@Resource(name = ShopService.BEAN_NAME)
	protected ShopService shopService;
	
	
	@Resource(name = cn.jekun.shop.common.service.ShopService.BEAN_NAME)
	protected cn.jekun.shop.common.service.ShopService shopAppService;
	/**
	 * 根据地理位置展示商家
	 * @return
	 */
	public String shopList(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");//页大小
			String currentPage = this.request.getParameter("currentPage");//当前页
			String typeId = this.request.getParameter("typeId");//商家类型编码
			String lng = this.request.getParameter("lng");//经度
			String lat = this.request.getParameter("lat");//纬度
			String cityId = this.request.getParameter("cityId");//已开通城市编码
			String shopName = this.request.getParameter("shopName");//商家名称
			String distance = this.request.getParameter("distance"); //距离
			String sort= this.request.getParameter("sort"); //排序
			String businessStatus=this.request.getParameter("businessStatus"); //营业状态
			
		/*	if(shopName!=null && !"".equals(shopName)){
				shopName = new String (this.request.getParameter("shopName").getBytes("iso-8859-1"),"utf-8");
				if(shopName.getBytes().length==shopName.length())
					shopName = new String (this.request.getParameter("shopName").getBytes("ISO8859_1"),"GBK");
//				shopName = new String (this.request.getParameter("shopName").getBytes("iso-8859-1"),"utf-8");
			}*/
			String regionId = this.request.getParameter("regionId");//地区代码
			jsonObject = shopService.queryList(pageSize,currentPage,distance,typeId,lng,lat,cityId,shopName,regionId,sort,businessStatus);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}
	
	/**
	 * 查询N个分类的N个商家列表
	 * @return
	 */
	public String getShopListByType(){
		
		
		JSONObject jsonObject = null;
		try{

			String shopType = this.request.getParameter("shopType");
			String lng = this.request.getParameter("lng");
			String lat = this.request.getParameter("lat");
			jsonObject = shopService.getShopListByType(lng,lat,shopType);
			
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	
	
	
	/**
	 * 商家商品展示
	 * @return
	 */
	public String goodsListByShopId(){
		JSONObject jsonObject = new JSONObject();
		try{
			String shopId = this.request.getParameter("shopId");
			jsonObject = shopService.showGoodsTypesByShopId(shopId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}
	
	/**
	 * 开店保存商家信息
	 * @return
	 */
	public String openShop(){
		JSONObject jsonObject = null;
		try{
			String shopName = request.getParameter("shopName");//商店名称
			String identityNo = request.getParameter("identityNo");//身份证号码
			String identityImage1 = request.getParameter("identityImage1");//身份证图片A
			String identityImage2 = request.getParameter("identityImage2");//身份证图片B
			String identityImage3 = request.getParameter("identityImage3");//身份证图片C
			String provinceId = request.getParameter("provinceId");//省份代码
			String cityId = request.getParameter("cityId");//城市代码
			String regionId = request.getParameter("regionId");//区域代码
			String address = request.getParameter("address");//详细地址
			String street = request.getParameter("street");//街道
			String longitude = request.getParameter("longitude");//经度
			String latitude = request.getParameter("latitude");//纬度
			String phone = request.getParameter("phone");//联系电话
			String mobile = request.getParameter("mobile");//联系手机
			String psw = request.getParameter("payPassword");//商家支付密码
			String shopTypeId = request.getParameter("shopTypeId");//商家支付密码
			String email = request.getParameter("email");
			String range = request.getParameter("range");//配送范围
			
			String certificate1 = request.getParameter("certificate1");//证件1
			String certificate2 = request.getParameter("certificate2");//证件2
			String certificate3 = request.getParameter("certificate3");//证件3
			String certificate4 = request.getParameter("certificate4");//证件4
			String certificate5 = request.getParameter("certificate5");//证件5
			
			
			if(!isLoginWeb()){
				return timeoutWeb();
			}
			//this.validateSign(shopName,identityNo);
			
			jsonObject = this.shopAppService.openShop(shopName,identityNo,identityImage1,identityImage2,identityImage3,
					provinceId,cityId,regionId,address,street,longitude,latitude,phone,mobile,psw,
					certificate1,certificate2,certificate3,certificate4,certificate5,getUserId(),shopTypeId,email,range);
			
			if(jsonObject.getString("code").equals("1")){
				
				
				MemberSession memberSession = (MemberSession) this.request.getSession().getAttribute(MemberSession.SESSION_KEY);

				memberSession.setShopId(jsonObject.getString("shopId"));
					
				memberSession.setShopName(jsonObject.getString("shopName"));

				this.request.getSession().setAttribute(MemberSession.SESSION_KEY, memberSession);

				
			}
			
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "网站用户开店");//记录操作日志
			
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 关注商家
	 * @return
	 */
	public String concernShop(){
		JSONObject jsonObject = null;
		try{
			
			if(!isLoginWeb()){
				return timeoutWeb();
			}
			String shopId = this.request.getParameter("shopId");
			jsonObject = shopAppService.concernShop(getUserId(),shopId);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "关注商家"+shopId+"成功，来源:web");//记录操作日志

		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "关注商家失败，原因:"+jsonObject.get("message")+",来源:web");//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 取消关注商家
	 * @return
	 */
	public String cancleConcernShop(){
		JSONObject jsonObject = null;
		try{
			if(!isLoginWeb()){
				return timeoutWeb();
			}
			String shopId = this.request.getParameter("shopId");
			jsonObject = shopAppService.cancleConcernShop(getUserId(),shopId);
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "取消关注商家"+shopId+"成功，操作来源:"+this.getUserId());//记录操作日志

		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			this.logService.addMallLog(this.getUserId(), 
					this.getBrowser(), this.getIp(), "取消关注商家失败，原因:"+jsonObject.get("message")+",操作来源:"+this.getUserId());//记录操作日志

		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	public String getIsOpenShop(){
		JSONObject jsonObject = null;
		try{
			
			jsonObject = shopService.getIsOpenShop(getUserId());
		
			this.request.getSession().setAttribute("shopId", jsonObject.getString("shopId"));
			
			this.request.getSession().setAttribute("shopName", jsonObject.getString("shopName"));
			
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	public String isShopExits(){
		JSONObject jsonObject = null;
		try{
			
			String shopId = this.request.getParameter("shopId");
			
			jsonObject = shopService.isShopExits(shopId);
		
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 *  获取商家的相册
	 *  @return
	 *
	 */
	public String getShopPic(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");
			
			jsonObject = this.shopAppService.getShopPics(shopId);
			
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
	public String getShopNameById(){
		JSONObject jsonObject = null;
		try{
			
			String shopId = this.request.getParameter("shopId");
			
			jsonObject = shopService.getShopNameById(shopId);
		
			
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
}
